package com.wwc.sc.controller;

import com.wwc.sc.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author WWC
 */
@RestController
public class DeptConsumerController {
    //服务消费者 没有service层
    //RestTemplate 供我们调用，但是需要注入到bean中
    @Autowired
    private RestTemplate restTemplate; //提供简单便捷的访问远程http请求的方法，简单的Restful模板

//    private static final String REST_URL_PREFIX = "http://localhost:8001";
//    通过ribbon实现的时候，应该通过同一个服务名去选择
    private static final String REST_URL_PREFIX = "http://SC-PROVIDER-DEPT";

    @RequestMapping("/consumer/dept/addDept")
    public int addDept(@RequestBody Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/addDept/",dept,int.class);
    }

    @RequestMapping("/consumer/dept/queryDept/{deptNo}")
    public Dept queryDept(@PathVariable("deptNo") String deptNo){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/queryDept/"+deptNo,Dept.class);
    }

    @RequestMapping("/consumer/dept/queryAllDept")
    public List<Dept> queryAllDept(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/queryAllDept",List.class);
    }
}
